package avicit.bdp.dcs.semistructure.rest;

import avicit.bdp.dcs.semistructure.dto.FileTemplateDTO;
import avicit.bdp.dcs.semistructure.dto.TemplateFieldDTO;
import avicit.bdp.dcs.semistructure.service.FileTemplateService;
import avicit.bdp.dcs.semistructure.service.TemplateFieldService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间：2023-04-03 14:10
 * @类说明：文件模板管理
 * @修改记录：
 */
@RestController
@RequestMapping("/api/bdp/dcs/template/FileTemplateRest")
@Api(tags = "新文件模板管理接口")
public class FileTemplateRest {

    private static final Logger logger = LoggerFactory.getLogger(FileTemplateRest.class);

    @Autowired
    private FileTemplateService templateService;
    @Autowired
    private TemplateFieldService templateFieldService;

    @GetMapping("/getTemplateList")
    @ApiOperation(value = "查询模板列表")
    public ResponseMsg<QueryRespBean<FileTemplateDTO>> getTemplateList(@ApiParam(value = "排序，按创建时间倒序：1，按创建时间升序：2，按名称升序：3，按名称倒序：4", name = "sort")  @RequestParam(required = false) Integer sort,
                                                                       @ApiParam(value = "模板名称", name = "templateName") @RequestParam(required = false) String templateName,
                                                                       @ApiParam(value = "空间ID", name = "projectId") @RequestParam(required = false) String projectId,
                                                                       @ApiParam(value = "状态,", name = "status") @RequestParam(required = false) Integer status,
                                                                       @ApiParam(value = "当前页", name = "pageNo", required = true)  @RequestParam(defaultValue = "1") Integer pageNo,
                                                                       @ApiParam(value = "每页显示条数", name = "pageSize", required = true) @RequestParam(defaultValue = "10") Integer pageSize) {
        ResponseMsg<QueryRespBean<FileTemplateDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(templateService.getTemplateList(sort, templateName, projectId, status, pageNo, pageSize));
        return responseMsg;
    }

    @PostMapping("/createTemplate")
    @ApiOperation(value = "新增模板")
    public ResponseMsg createTemplate(@ApiParam(value = "FileTemplateDTO", name = "FileTemplateDTO")
                                      @RequestBody @Valid FileTemplateDTO templateDTO) throws Exception {
        ResponseMsg responseMsg = new ResponseMsg<>();
        templateService.createTemplate(templateDTO);
        return responseMsg;
    }

    @PostMapping("/updateTemplate")
    @ApiOperation(value = "修改模板")
    public ResponseMsg updateTemplate(@ApiParam(value = "FileTemplateDTO", name = "FileTemplateDTO")
                                      @RequestBody @Valid FileTemplateDTO templateDTO) throws Exception {
        ResponseMsg responseMsg = new ResponseMsg<>();
        templateService.updateTemplate(templateDTO);
        return responseMsg;
    }

    @DeleteMapping("/deleteTemplateByIds")
    @ApiOperation(value = "删除模板")
    public ResponseMsg<Boolean> deleteTemplateByIds(@ApiParam(value = "主键id(多个逗号分隔)", name = "templateIds", required = true)
                                                    @RequestBody FileTemplateDTO templateDTO) throws Exception{
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        templateService.deleteTemplateByIds(templateDTO);
        return responseMsg;
    }

    @GetMapping("/getTemplateById")
    @ApiOperation(value = "查看模板")
    public ResponseMsg<FileTemplateDTO> getTemplateById(@ApiParam(value = "模板ID", name = "templateId")  @RequestParam(required = true) String templateId) throws Exception {
        ResponseMsg<FileTemplateDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(templateService.getTemplateById(templateId));
        return responseMsg;
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "启用/停用")
    public ResponseMsg<Boolean> updateStatus(@RequestBody FileTemplateDTO templateDTO) throws Exception {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(templateService.updateStatus(templateDTO.getId(), templateDTO.getStatus()));
        return responseMsg;
    }

    @GetMapping("/getTemplateFieldList")
    @ApiOperation(value = "查询模板字段列表")
    public ResponseMsg<QueryRespBean<TemplateFieldDTO>> getTemplateFieldList(@ApiParam(value = "模板ID", name = "templateId")  @RequestParam(required = true) String templateId,
                                                                             @ApiParam(value = "模板名称", name = "name") @RequestParam(required = false) String name,
                                                                             @ApiParam(value = "当前页", name = "pageNo", required = true)  @RequestParam(defaultValue = "1") Integer pageNo,
                                                                             @ApiParam(value = "每页显示条数", name = "pageSize", required = true) @RequestParam(defaultValue = "10") Integer pageSize) {
        ResponseMsg<QueryRespBean<TemplateFieldDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(templateFieldService.getTemplateFieldList(templateId, name, pageNo, pageSize));
        return responseMsg;
    }

    @GetMapping("/downloadTemplate")
    @ApiOperation(value = "下载标准模板")
    public void downloadTemplate(HttpServletResponse response) {
        templateFieldService.downloadTemplate(response);
    }

    @PostMapping("/upload")
    @ApiOperation(value = "模板文件上传")
    public ResponseMsg<List<TemplateFieldDTO>> upload(@ApiParam(value = "FileTemplateDTO", name = "FileTemplateDTO") FileTemplateDTO templateDTO) throws Exception{
        ResponseMsg<List<TemplateFieldDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(templateService.upload(templateDTO));
        return responseMsg;
    }

    @PostMapping(value = "/importTemplate")
    @ApiOperation(value = "导入文件模板")
    public ResponseMsg importTemplate(@ApiParam(value = "FileTemplateDTO", name = "FileTemplateDTO") FileTemplateDTO templateDTO) {
        ResponseMsg responseMsg = new ResponseMsg<>();
        templateService.importTemplate(templateDTO);
        return responseMsg;
    }

    @GetMapping(value = "/exportTemplate")
    @ApiOperation(value = "导出文件模板")
    public void exportTemplate(@ApiParam(value = "模板ID", name = "id") @RequestParam(value = "id") String id,
                               HttpServletResponse response) {
        templateService.exportTemplate(id, response);
    }

}
